/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author DongVan
 */
public class Ve {
    private int m_IDVe;
	private int m_IDTuyen;
	private String m_NgayDi;
	private int m_TinhTrang;



	public int getM_TinhTrang() {
		return m_TinhTrang;
	}
	public void setM_TinhTrang(int m_TinhTrang) {
		this.m_TinhTrang = m_TinhTrang;
	}
	public int getM_IDVe() {
		return m_IDVe;
	}
	public void setM_IDVe(int m_IDVe) {
		this.m_IDVe = m_IDVe;
	}
	public int getM_IDTuyen() {
		return m_IDTuyen;
	}
	public void setM_IDTuyen(int m_IDTuyen) {
		this.m_IDTuyen = m_IDTuyen;
	}
	public String getM_NgayDi() {
		return m_NgayDi;
	}
	public void setM_NgayDi(String m_NgayDi) {
		this.m_NgayDi = m_NgayDi;
	}
	
	public Ve(int m_IDVe, int m_IDTuyen, String m_NgayDi, int m_TinhTrang) {
		this.m_IDVe = m_IDVe;
		this.m_IDTuyen = m_IDTuyen;
		this.m_NgayDi = m_NgayDi;
		this.m_TinhTrang = m_TinhTrang;
	}
	
	public Ve(){
		
	}
	
	
	AESEncrypt aes = new AESEncrypt();
	Datapro dataprovider = new Datapro();
	
	
	public ArrayList<Ve> selected() {
		ArrayList<Ve> listVe = new ArrayList<Ve>();
		String sqlQuery = "select * from VE";
		ResultSet rs = dataprovider.Selected(sqlQuery);
		try {
			while (rs.next()) {
				Ve itemVe = new Ve();
				itemVe.m_IDVe = rs.getInt(1);
				itemVe.m_IDTuyen= rs.getInt(2);
				itemVe.m_NgayDi = aes.decryptString(rs.getString(3));
				itemVe.m_TinhTrang = rs.getInt(4);
				listVe.add(itemVe);
			}
			dataprovider.disconnect();
	        rs.close();
	        return listVe;
	        
	    } catch ( SQLException ex) {
	        Logger.getLogger(Datapro.class.getName()).log(Level.SEVERE, null, ex);
	    }
		return null;
	}
	

	public boolean insert() {
		int idTuyen = this.m_IDTuyen;
		String ngayDi = aes.encryptString(this.m_NgayDi);
		int tinhTrang = this.m_TinhTrang;
		String query =  "insert into VE values("+idTuyen+",'"+ ngayDi + "',"+tinhTrang+")";
		boolean result =  dataprovider.Insert(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean update(){
		int idTuyen = this.m_IDTuyen;
		String ngayDi = aes.encryptString(this.m_NgayDi);
		int tinhTrang = this.m_TinhTrang;
		String query = "update VE set IDTuyen = "+ idTuyen +", NgayDi = '"+ngayDi+"', TinhTrang = "+tinhTrang+" where IDLoaiND = " + this.m_IDVe + "\"";
		boolean result = dataprovider.Update(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean delete(){
		String query = "delete from VE where = " + this.m_IDVe + "\"";
		boolean result = dataprovider.Delete(query);
		dataprovider.disconnect();
		return result;
	}
}
